#include <string>
#include "atomic.h"
#include "message.h"
#include "mainsimu.h"
#include "strutil.h"
#include "../HeadFile/ERP_GB_processing_1_model.h"


/**** the Constructor ****/
ERP_GB_processing_1_model::ERP_GB_processing_1_model( const string &name)
: Atomic( name )
, ERP_GB_1_stream_MTBE( this->addInputPort( "ERP_GB_1_stream_MTBE") )
, ERP_GB_1_stream_LNA( this->addInputPort( "ERP_GB_1_stream_LNA") )
, ERP_GB_1_stream_GASO( this->addInputPort( "ERP_GB_1_stream_GASO") )
, ERP_GB_1_stream_mode( this->addInputPort( "ERP_GB_1_stream_mode") )
, ERP_GB_1_stream_HOGO( this->addInputPort( "ERP_GB_1_stream_HOGO") )
, ERP_GB_1_stream_GB93( this->addOutputPort( "ERP_GB_1_stream_GB93") )
, ERP_GB_1_stream_OP( this->addOutputPort( "ERP_GB_1_stream_OP") )
, ERP_GB_1_stream_GB90( this->addOutputPort( "ERP_GB_1_stream_GB90") )
{

		
	//	string tmpval1(MainSimulator::Instance().getParameter( this->description(), "ERP_GB_1_stream_LNA_yield_val" ));
	//	string tmpval2(MainSimulator::Instance().getParameter( this->description(), "ERP_GB_1_stream_HOGO_yield_val" ));
	//	string tmpval3(MainSimulator::Instance().getParameter( this->description(), "ERP_GB_1_stream_GASO_yield_val" ));
	//	string tmpval4(MainSimulator::Instance().getParameter( this->description(), "ERP_GB_1_stream_MTBE_yield_val" ));
	//	string tmpval5(MainSimulator::Instance().getParameter( this->description(), "ERP_GB_1_stream_GB93_yield_val" ));
	//	string tmpval6(MainSimulator::Instance().getParameter( this->description(), "ERP_GB_1_stream_GB90_yield_val" ));
	//
	//	ERP_GB_1_stream_LNA_yield_val = str2float(tmpval1);
	//	ERP_GB_1_stream_HOGO_yield_val = str2float(tmpval2);
	//	ERP_GB_1_stream_GASO_yield_val = str2float(tmpval3);
	//	ERP_GB_1_stream_MTBE_yield_val = str2float(tmpval4);
	//	ERP_GB_1_stream_GB93_yield_val = str2float(tmpval5);
	//	ERP_GB_1_stream_GB90_yield_val = str2float(tmpval6);

		
		
}


/*** the initFunction ***/
Model &ERP_GB_processing_1_model::initFunction()
{

		
	total_flowrate = 0;
	count = 0;
	count = 0;
	delay = "00:00:00:00";
	passivate();
	return *this;
		
		
}

/*** the externalFunction ***/
Model &ERP_GB_processing_1_model::externalFunction( const ExternalMessage &msg )
{

			
	if(msg.port() == ERP_GB_1_stream_mode)
	{
		ERP_GB_1_stream_mode_val = msg.value();
		if(ERP_GB_1_stream_mode_val == 0)
		{
			ERP_GB_1_stream_LNA_yield_val = 1.0;
			ERP_GB_1_stream_HOGO_yield_val = 1.0;
			ERP_GB_1_stream_GASO_yield_val = 1.0;
			ERP_GB_1_stream_MTBE_yield_val = 1.0;
			ERP_GB_1_stream_GB90_yield_val = 1.0;
			ERP_GB_1_stream_GB93_yield_val = 0.0;
		}
		else
		{
			ERP_GB_1_stream_LNA_yield_val = 1.0;
			ERP_GB_1_stream_HOGO_yield_val = 1.0;
			ERP_GB_1_stream_GASO_yield_val = 1.0;
			ERP_GB_1_stream_MTBE_yield_val = 1.0;
			ERP_GB_1_stream_GB90_yield_val = 0.0;
			ERP_GB_1_stream_GB93_yield_val = 1.0;
		}
	}

	if( msg.port() == ERP_GB_1_stream_LNA ){
		ERP_GB_1_stream_LNA_flowrate = msg.value();
		holdIn( active, delay);
		count++;
		return *this;
	}
	if( msg.port() == ERP_GB_1_stream_HOGO ){
		ERP_GB_1_stream_HOGO_flowrate = msg.value();
		holdIn( active, delay);
		count++;
		return *this;
	}
	if( msg.port() == ERP_GB_1_stream_GASO ){
		ERP_GB_1_stream_GASO_flowrate = msg.value();
		holdIn( active, delay);
		count++;
		return *this;
	}
	if( msg.port() == ERP_GB_1_stream_MTBE ){
		ERP_GB_1_stream_MTBE_flowrate = msg.value();
		holdIn( active, delay);
		count++;
		return *this;
	}
	return *this;
		
		
}

/*** the internalFunction ***/
Model &ERP_GB_processing_1_model::internalFunction( const InternalMessage &msg )
{

			
	passivate();
	return *this;
		
		
}

/*** the outputFunction ***/
Model &ERP_GB_processing_1_model::outputFunction( const InternalMessage &msg )
{

			
	if(count == 4)
	{
		total_flowrate += ERP_GB_1_stream_LNA_flowrate;
		total_flowrate += ERP_GB_1_stream_HOGO_flowrate;
		total_flowrate += ERP_GB_1_stream_GASO_flowrate;
		total_flowrate += ERP_GB_1_stream_MTBE_flowrate;
		ERP_GB_1_stream_GB90_flowrate = total_flowrate * ERP_GB_1_stream_GB90_yield_val;
		ERP_GB_1_stream_GB93_flowrate = total_flowrate * ERP_GB_1_stream_GB93_yield_val;
		sendOutput( msg.time(), ERP_GB_1_stream_OP, total_flowrate);
		sendOutput( msg.time(), ERP_GB_1_stream_GB90, ERP_GB_1_stream_GB90_flowrate);
		sendOutput( msg.time(), ERP_GB_1_stream_GB93, ERP_GB_1_stream_GB93_flowrate);
		total_flowrate = 0;
		count = 0;
	}
	return *this;
		
		
}

